package com.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.beans.AdminInfo;
import com.dao.AdminDao;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Resource
	private AdminDao adminDao;
	@RequestMapping(value = {"/login"})
	public String go(ModelMap map,HttpSession sessions,String adminName,String password) {
		/* adminDao.findByAdminNameAndPassword(name, pwd); */
		System.out.println(adminDao.getOne(3));
		AdminInfo admin= adminDao.findByAdminNameAndPassword(adminName, password);
		if(admin!=null) {sessions.setAttribute("g_admin", admin);	return "redirect:/index.html";}
		else { 
			map.put("msg", "用户名或密码错误,登录失败"); 
			return "/login.jsp";
		}
	}
	@RequestMapping(value = {""})
	public String goLogin() {
		return "/login.jsp";
	}
	@RequestMapping("/logout")
	public void logout(HttpSession session,HttpServletResponse response) throws IOException {
		session.removeAttribute("g_admin");	
		session.removeAttribute("studentList");
		response.getWriter().print("<script>window.top.location.href='/login.jsp'</script>");
	}
	
	//刷session 防止过期
	@GetMapping("/refresh")
	public void refreshSession(HttpSession session){
		AdminInfo admin =(AdminInfo)session.getAttribute("g_admin");	
		session.setAttribute("g_admin",admin);
	}
}
